﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace TibcoMessageAdmin
{
    public partial class Config : Form
    {
        string file = Application.StartupPath +"\\"  + "config.xml"  ;
        public string currentID = "";
        public Config()
        {
            InitializeComponent();
        }

        List<Queue> queueList = new List<Queue>();
        Queue currentQueue = null;
        private void Config_Load(object sender, EventArgs e)
        {
            Util.LoadQueues(cboConfig, ref queueList);
        }

     




        private void cboConfig_SelectedIndexChanged(object sender, EventArgs e)
        {
            currentID = ((QueueDetail)cboConfig.SelectedItem).ID;
            currentQueue = queueList.Find(xPath => xPath.ID == currentID);
            if (currentQueue != null)
            {
                txtServer.Text = currentQueue.Server;
                txtQueue.Text = currentQueue.QueueName;
                txtUser.Text = currentQueue.User;
                txtPwd.Text = currentQueue.Password;
            }

        }

        private void cmdSave_Click(object sender, EventArgs e)
        {
            queueList.Remove(currentQueue);
            currentQueue.Server = txtServer.Text;
            currentQueue.QueueName = txtQueue.Text;
            currentQueue.User = txtUser.Text;
            currentQueue.Password = txtPwd.Text;
            queueList.Add(currentQueue);


            try
            {
                XmlDocument doc = new XmlDocument();
                // create a writer and open the file
                TextWriter tw = new StreamWriter(file, false, Encoding.UTF8);
                tw.WriteLine("<?xml version=\"1.0\" ?>");
                tw.WriteLine("<QueueDetails>");

                // write a line of text to the file
                foreach (Queue q in queueList)
                {

                    tw.WriteLine(q.ToString());

                }
                tw.WriteLine("</QueueDetails>");
                tw.Close();
                Util.LoadQueues(cboConfig, ref queueList);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);   

            }
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            //queueList.Remove(currentQueue);
            Queue currentQueue = new Queue();
            currentQueue.ID = new Guid().ToString("N"); 
            currentQueue.Server = txtServer.Text;
            currentQueue.QueueName = txtQueue.Text;
            currentQueue.User = txtUser.Text;
            currentQueue.Password = txtPwd.Text;
            queueList.Add(currentQueue);

            try
            {
                XmlDocument doc = new XmlDocument();
                // create a writer and open the file
                TextWriter tw = new StreamWriter(file, false, Encoding.UTF8);
                tw.WriteLine("<?xml version=\"1.0\" ?>");
                tw.WriteLine("<QueueDetails>");

                // write a line of text to the file
                foreach (Queue q in queueList)
                {

                    tw.WriteLine(q.ToString());

                }
                tw.WriteLine("</QueueDetails>");
                tw.Close();
                Util.LoadQueues(cboConfig, ref queueList);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);

            }
        }

       
    }
}
